package kr.hkit.password.fragment.account;

import kr.hkit.password.R;
import kr.hkit.password.dao.ChangePassword;
import kr.hkit.password.dao.InfoDao;
import kr.hkit.password.db.MyDatabaseManager;
import android.app.Fragment;
import android.content.ContentValues;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class AccountCreate extends Fragment {
	private static final String TAG = "PW";
	private EditText eAccount;
	private EditText ePassword;
	private EditText eAnswer;
	private TextView tvCheck;
	private MyDatabaseManager mAdb;
	private ArrayAdapter<CharSequence> adspin;
	private boolean isSelect;
	private int hintPosition;
	private Spinner spin;
	private Button btn;
	private Button btnRC;
	private InfoDao info;
	private ChangePassword cp;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(TAG, "AccountCreate onCreateView");
		View view = inflater.inflate(R.layout.account_create, container, false);
		info = (InfoDao) getActivity().getApplication();
		info.setMain(true);
		tvCheck = (TextView) view.findViewById(R.id.tvCheck);
		eAccount = (EditText) view.findViewById(R.id.account);
		ePassword = (EditText) view.findViewById(R.id.password);
		ePassword.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				cp = new ChangePassword(ePassword.getText().toString());
				tvCheck.setText(cp.getCheck());
				if (cp.getCheck() == "위험") {
					tvCheck.setTextColor(Color.RED);
				} else if (cp.getCheck() == "보통") {
					tvCheck.setTextColor(Color.YELLOW);
				} else {
					tvCheck.setTextColor(Color.GREEN);
				}
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,	int after) {}
			@Override
			public void afterTextChanged(Editable s) {}
		});
		eAnswer = (EditText) view.findViewById(R.id.answer);
		btnRC = (Button) view.findViewById(R.id.btnRC);
		btnRC.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				cp = new ChangePassword();
				ePassword.setText(cp.getRandomPw());				
			}
		});
		
		btn = (Button) view.findViewById(R.id.btnAdd);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d(TAG, "AccountCreate onClick");
				if (isSelect == true) {
					ContentValues addRowValue = new ContentValues();
					addRowValue.put("account", eAccount.getText().toString());
					addRowValue.put("password", ePassword.getText().toString());
					addRowValue.put("hint", hintPosition);
					addRowValue.put("answer", eAnswer.getText().toString());
					addRowValue.put("alarm", 1);
					mAdb = MyDatabaseManager.getInstance(getActivity());
					mAdb.insert("account", addRowValue);
					getFragmentManager().beginTransaction()
							.replace(R.id.content_frame, new AccountLogin()).commit();
					Toast.makeText(getActivity(), "Success!!",
							Toast.LENGTH_LONG).show();
				} else {
					Toast.makeText(getActivity(), "Fail", Toast.LENGTH_LONG)
							.show();
				}
			}
		});

		spin = (Spinner) view.findViewById(R.id.spinner);
		spin.setPrompt("힌트를 선택하세요.");

		adspin = ArrayAdapter.createFromResource(getActivity(), R.array.hint,
				android.R.layout.simple_spinner_item);
		adspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(adspin);

		spin.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				isSelect = true;
				hintPosition = position;
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}

		});
		return view;
	}
}
